package com.canbot.u05.adapter;

import android.app.ActionBar;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.PresideTaskRunActivity;
import com.canbot.u05.bean.AdvisoryQA;
import com.canbot.u05.utils.Logger;

import java.util.Collections;
import java.util.List;

/**
 * 咨询问答设置数据界面
 */

public class AdvisoryAdapter extends RecyclerView.Adapter<AdvisoryAdapter.ViewHolder> implements View.OnClickListener {

        private Context context;

        private List<AdvisoryQA> data;

        private LayoutInflater mInflater;

        private PopupWindow popupWindow;

        private RecyclerView listView;

        private static final String TAG = "AdvisoryAdapter";
        public AdvisoryAdapter(Context context, List<AdvisoryQA> data, RecyclerView listView) {
                this.context = context;
                this.listView = listView;
                this.mInflater = LayoutInflater.from(context);
                this.data = Collections.synchronizedList(data);

                Logger.e(TAG,"data = " +data.toString());
        }

        /**
         * 弹窗显示菜单
         *
         * @param view
         * @param position
         */
        public void showPopupWindow(View view, int position) {
                 Logger.e(TAG,"showPopupWindow = " + position);
                View contentview = LayoutInflater.from(context).inflate(R.layout.menu_setting_advisory, null);

                // 设置按钮的点击事件
                Button btnEditQue = (Button) contentview.findViewById(R.id.btn_edit_question);
                Button btnEditAnswer = (Button) contentview.findViewById(R.id.btn_edit_answer);
                Button btnDel = (Button) contentview.findViewById(R.id.btn_del);

                btnEditQue.setOnClickListener(this);
                btnEditAnswer.setOnClickListener(this);
                btnDel.setOnClickListener(this);

                // 显示具体名称
                TextView textView = (TextView) contentview.findViewById(R.id.title);
                textView.setText(data.get(position).getQuestion());

                popupWindow = new PopupWindow(contentview,
                        ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);

                popupWindow.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Log.i("mengdd", "onTouch : ");
                                return false;
                                // 这里如果返回true的话，touch事件将被拦截
                                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

                        }
                });

                int visibleLastPosi = -1;
                RecyclerView.LayoutManager layoutManager = listView.getLayoutManager();
                //判断是当前layoutManager是否为LinearLayoutManager
                // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
                if (layoutManager instanceof LinearLayoutManager) {
                        LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                        //获取最后一个可见view的位置
                        visibleLastPosi = linearManager.findLastVisibleItemPosition();
                }
                // 设置好参数之后再show
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                if (position <= visibleLastPosi-4){
                        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                                R.drawable.menu_background));
                        popupWindow.showAsDropDown(view,-(int)context.getResources().getDimension(R.dimen.x279),-(int)context.getResources().getDimension(R.dimen.x40));
                }else {
                        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                                R.drawable.menu_background2));
                        popupWindow.showAsDropDown(view,-(int)context.getResources().getDimension(R.dimen.x279),-(int)context.getResources().getDimension(R.dimen.x260));

                }
//                popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0] - 220 - 40 - 20 - 10, location[1] - 13);
                if (listner != null) {
                        listner.showPopuwindow(position);
                }


        }

        AdvisoryAdapterOnclickListner listner;

        public void setListener(AdvisoryAdapterOnclickListner listner) {
                this.listner = listner;
        }

        public interface AdvisoryAdapterOnclickListner {

                void editQue();

                void editAnswer();

                //        void edit();
                void del();

                void showPopuwindow(int position);

        }

        @Override
        public void onClick(View view) {
                Logger.e(TAG,"onClick");
                switch (view.getId()) {
                        case R.id.btn_edit_question:

                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.editQue();
                                        }
                                }
                                break;
                        case R.id.btn_edit_answer:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.editAnswer();
                                        }
                                }
                                break;

                        case R.id.btn_del:

                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.del();
                                        }
                                }
                                break;
                }

        }

        class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

                RelativeLayout relativeLayout;
                public TextView tvQustion;
                public ImageView ivMenu;
                public int position;


                public ViewHolder(View itemView) {
                        super(itemView);
                        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.rl_item_onclick);
                        tvQustion = (TextView) itemView.findViewById(R.id.text1);
                        ivMenu = (ImageView)  itemView.findViewById(R.id.img1);
                        relativeLayout.setOnClickListener(this);
                }
                @Override
                public void onClick(View v) {
                         switch (v.getId()){

                                 case R.id.rl_item_onclick:
                                         showPopupWindow(ivMenu,position);
                                         break;
                         }
                }
        }


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View inflate = mInflater.inflate(R.layout.sales_setting_item, parent, false);
                return new ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
                holder.position = position;
                AdvisoryQA advisoryQA = data.get(position);
                holder.tvQustion.setText(advisoryQA.getQuestion());
                Logger.e(TAG,"onBindViewHolder position = " + position);
        }

        @Override
        public int getItemViewType(int position) {
                return data.get(position).getType();
        }
        @Override
        public int getItemCount() {
                return data.size();
        }

}
